package com.nstar.orderexpress.view
{
	import com.nstar.base.controls.AnimatedTileList;
	import com.nstar.base.util.EffectUtils;
	import com.nstar.orderexpress.control.OrderMenuListEvent;
	import com.nstar.orderexpress.control.OrderMenuThumbEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.mxml.ConfirmView;
	import com.nstar.orderexpress.mxml.OrderMenuList;
	import com.nstar.orderexpress.mxml.ToolTipPanel;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.Text;
	import mx.events.FlexEvent;
	
	public class ShoppingCartViewClass extends Canvas
	{
		public var omlSelected:OrderMenuList;
		public var btnClearAll:Button;
		public var btnOrder:Button;
		public var txtTotal:Text;
		
		public var atlLikeList:AnimatedTileList;
		
        [Bindable]
        public var numOrderMenus:int=0;		
        
        private var model:Model = Model.getInstance();
		
		public function ShoppingCartViewClass()
		{
			//TODO: implement function
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(OrderMenuThumbEvent.PURCHASE,purchaseHandler);
		}
		
		private function init(event:FlexEvent):void{
			btnClearAll.addEventListener(MouseEvent.CLICK,clearAllHandler);
			btnOrder.addEventListener(MouseEvent.CLICK,orderHandler);
			
			omlSelected.addEventListener(OrderMenuListEvent.ADD_MENU,orderMenuListHandler);
			omlSelected.addEventListener(OrderMenuListEvent.DUPLICATE_MENU,orderMenuListHandler);
			omlSelected.addEventListener(OrderMenuListEvent.MENU_QTY_CHANGE,orderMenuListHandler);
			omlSelected.addEventListener(OrderMenuListEvent.REMOVE_MENU,orderMenuListHandler);			
		}
		
		//订单处理
		private function orderHandler(event:MouseEvent):void{
			if (!validate()){
				ToolTipPanel.show("no menus selected!");
				return;
			}
			if (!model.mainApp.userView.validate()){
				ToolTipPanel.show("You must provide you name, mobile and address information!");
				return;
			}
			//订单信息确认
			ConfirmView.show();
		}
		
		//验证订单数据
		private function validate():Boolean{
			return numOrderMenus >0;
		}
		
		private function clearAllHandler(event:MouseEvent):void{
			numOrderMenus = 0;
			omlSelected.removeItemAll();
			txtTotal.text = omlSelected.statInfo();
		}
		
		private function purchaseHandler(event:*):void{
			txtTotal.text = omlSelected.statInfo();
			model.mainApp.tbbControl.selectedIndex = 1;
			createSampleDate();
		}
		
		//模拟些数据
		private function createSampleDate():void{
			var ary:ArrayCollection = new ArrayCollection();
			for(var i:int = 0;i< Math.round(5*Math.random())+1;i++){
				var item:Object = new Object();
				item.name = "menu"+i;
				ary.addItem(item);
			}
			atlLikeList.dataProvider = ary;
		}
		
		//菜单事件处理
		private function orderMenuListHandler(event:OrderMenuListEvent):void{
            switch (event.type)
            {
                case OrderMenuListEvent.ADD_MENU:
                    event.orderMenu.quantity = 0;  
                    //fall through into the same logic as dup          
                case OrderMenuListEvent.DUPLICATE_MENU:
                    event.orderMenu.quantity++;
                    numOrderMenus++;
                    break;            
                case OrderMenuListEvent.MENU_QTY_CHANGE:
                case OrderMenuListEvent.REMOVE_MENU:
                    var items:Array = omlSelected.items;
                    numOrderMenus = 0;
                    for (var i:int=0; i < items.length; i++)
                    {
                        var orderMenu:OrderMenuVO = items[i].orderMenu;
                        numOrderMenus += orderMenu.quantity;
                    }    
                    txtTotal.text = omlSelected.statInfo();
                    break;
                default:
                    break;
            }			
		}
	}
}